package com.mhs.tracuu;

import java.util.ArrayList;
import java.util.List;

import com.mhs.mhs_gui.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.mhs.BUS.ThuocBUS;
import com.mhs.DTO.MySQLiteHelper;
import com.mhs.DTO.ThuocDTO;
import com.mhs.moreadapter.MyListViewAdapter;

public class TraCuuThuoc extends Activity{
	MySQLiteHelper dbHelper;
	public final Context THIS = this;
	Intent intent = new Intent();
	ListView myList;
	List<ThuocDTO> lstIDThuoc = new ArrayList<ThuocDTO>();

	TextView selection;
	String[] items;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tracuuthuoc);

		//myList = (ListView) findViewById(R.id.lv_thuocvuaxem);
		myList = (ListView) findViewById(R.id.lv_thuocvuaxem);

		myList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parentView, View v, int position, long id) {
				Intent tt = new Intent(THIS, TraCuuThuoc_Search_Detail.class);
				int maThuoc = lstIDThuoc.get(position).getMATHUOC();
				tt.putExtra("maThuoc", maThuoc);
				startActivity(tt);
			}
		}); 

		//ImageView iv_search = (ImageView)findViewById(R.id.iv_search);
		setListViewAdapter();
	}

	@Override
	protected void onStart(){
		super.onStart();
		dbHelper = new MySQLiteHelper(this);

		setListViewAdapter();
	}

	private void setListViewAdapter(){
		ThuocBUS thuocBUS = new ThuocBUS();
		List<ThuocDTO> lstThuoc = thuocBUS.getAllInfoThuocDTO(this);
		
		ArrayList<String> lstTenThuoc = new ArrayList<String>();
		if(lstThuoc == null){
			Intent tt = new Intent(this, TraCuuThuoc_Search.class);
			startActivity(tt);
		}else{
			lstIDThuoc = lstThuoc;
			for(int i = 0; i < lstThuoc.size(); i++){
				ThuocDTO thuoc = lstThuoc.get(i);
				lstTenThuoc.add(thuoc.getTENTHUOC());
			}
			items = lstTenThuoc.toArray(new String[0]);

			//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, tenThuocs);
			//myList.setAdapter(adapter);	
			myList.setAdapter(new MyListViewAdapter(this, items, R.layout.myrow));
		}
	}


	public void StartSearchIntent(View v){
		Intent tt = new Intent(this, TraCuuThuoc_Search.class);
		startActivity(tt);
	}
	
	public void clearList(){
		lstIDThuoc.clear();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}



